<?php
  //============================================================================
  // ____________________________PLIGG TEAM ADDON_______________________________
  //
  // This file was created by AnAlienHolakres3  ||| XXXXX  XX     XX XXXX   XXXX
  // (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
  // @last_update                    26/02/2008 ||| XX     XX     XX X  XX  X  XX
  // @version                              0.8  ||| XX     XXXXXX XX XXXXX  XXXXX
  //
  // This file can be used by Pligg community.
  //=============================================================================
  
 // This is far better solution than to add code to class Link, because in that
 // case when EVERY link is loaded, task sql would be requested. In this solution
 // is sql query requested only when user desires to do so   
  
  if ($_POST['link_id']){
                    
  
    include_once('Smarty.class.php');
    $main_smarty = new Smarty;
    
    include('config.php');
    include(mnminclude . 'team.php');
    include(mnminclude . 'tasklinkship.php');
    include(mnminclude . 'task.php'); 
    include(mnminclude . 'teamlogs.php'); 
    
    
    include(mnminclude.'html1.php');  
    include(mnminclude.'smartyvariables.php');                  
        
   // if($current_user->user_id != $_POST['user_id']) {        
   //     echo "Hack attempt - bad user!";
    //    die;    
    //}
    $team = new Team;
    $task = new Task;  
    $tasklinkship = new TaskLinkhip;
    
    $team->check_id($_POST['link_id']);
    
  //request for user tasks
    if (!$_POST['task_id'] && !$_POST['team_id']){
    
      
        $task->read_user_tasks();
        $user_tasks = $task->get_user_tasks();
        
        $tasklinkship->read_user_tasklinks();
        $tasklinkship->fill_print();
        
        if ($user_tasks){
                                                         
            foreach ($user_tasks as $taskres){  
                         
             $task_id[] =$taskres->task_id;
             $team_id[] = $taskres->team_id;
             $task_user_id[] = $taskres->task_worker_id;
             $task_name[]=$taskres->task_name;
             $team_name[]=$taskres->team_name;
             
            }      
            $main_smarty->assign('link_id',$_POST['link_id']);
            $main_smarty->assign('task_ids',$task_id);
            $main_smarty->assign('team_ids',$team_id);            
            $main_smarty->assign('task_user_ids',$task_user_id);
            $main_smarty->assign('task_names',$task_name);
            $main_smarty->assign('team_names',$team_name);
                                    
            $main_smarty->display($the_template . '/tasklink_mini.tpl');     
        } else {echo "<div class='error' style='background:#fff9f9'>{$main_smarty->get_config_vars('PLIGG_No_Team_Tasks_In')}</div>";}
            
     // request for saving tasklink            
    } else {
        
      $team->check_id($_POST['task_id']);
      $team->check_id($_POST['team_id']);
      
       $tasklinkship->set_team_id($_POST['team_id']);
       $tasklinkship->set_task_id($_POST['task_id']);
       $tasklinkship->set_link_id($_POST['link_id']);
       $tasklinkship->set_user_id($current_user->user_id);
      
      if (!$tasklinkship->tasklink_already_added()) {           
       if (!$tasklinkship->save_tasklink()){
        echo "<div style='float:left;color:red;font-weight:bold'>{$main_smarty->get_config_vars('PLIGG_Team_Tasklink_Error')}</div>";
       } else {
       $teamlog = new TeamLog;       
       $teamlog->save_teamlog_values(9,$_POST['task_id'],$current_user->user_id,null);
        echo "<div style='float:left;color:#53ff00;font-weight:bold'>OK</div>";   
           
       }
      } else { 
         echo "<div style='float:left;color:red;font-weight:bold'>{$main_smarty->get_config_vars('PLIGG_Team_Tasklink_Already_Added')}</div>";   
       
          
      }
    }
                
             
  }  
    
  

?>
